package ssh.shop.action;


import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import ssh.shop.entity.ReturnEmployee;
import ssh.shop.entity.User;

import java.util.ArrayList;
import java.util.List;

/*https://www.cnblogs.com/yuwenlanleng/p/6596194.html*/
@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction<User> {

    private static Logger logger = Logger.getLogger(UserAction.class);

    private String tip;

    public String getTip() {
        return tip;
    }

    public void setTip(String tip) {
        this.tip = tip;
    }



    public String save() {
        System.out.println("---save---");
        System.out.println(model);
        userService.saveUser(model);
        logger.info("---完成用户信息插入---"+model);
        return query();
    }

    public String edit() {
        System.out.println("---修改数据--"+model.getId());
        String param = getParam("param");
        if(param != null){
//            userService.update(model);
            userService.queryUser(model);
            logger.info("---修改用户信息---"+model);
        }else{
            User user = userService.getUserById("1");
            session.put("user", user);
            return "edit";

        }

        return query();
    }

    protected String getParam(String key){
        return ServletActionContext.getRequest().getParameter(key);
    }



    public String del() {
        System.out.println("---del;---");
        System.out.println(model);
//        userService.del(model.getID().toString());
        userService.delUser(model);
        System.out.println("删除数据"+model);

        return query();

    }

    public String query() {
//        List<ReturnEmployee> userList = userService.userListB();
//        session.put("userList", userList);
//        for(User user:userList){
//            System.out.println("username==========="+user.getUserid());
//        }

        List<Object[]> lists = userService.userListObjec();
        List<ReturnEmployee> userList = new ArrayList<>();
        for(int i=0;i<lists.size();i++){
            ReturnEmployee employee = new ReturnEmployee();
            Object[] object = (Object[]) lists.get(i);
            employee.setId(Integer.parseInt(object[0].toString()));
            employee.setPassword(object[1].toString());
            employee.setPic(object[2].toString());
            employee.setUserid(object[3].toString());
            employee.setType(object[4].toString());
            employee.setName(object[5].toString());
            userList.add(employee);
        }
        session.put("userList", userList);
        return "success";
    }

    public String toMain() {
        User user = (User) session.get("user");
        System.out.println("session:" + user.toString());
        if (user != null)
            return "success";
        else {
            return "error";
        }
    }

    public String login() {
        System.out.println("进入登陆!");
        if (model.getUserid() == null || model.getUserid().equals("") && model.getPassword() != null && !model.getPassword().equals("")) {

            tip = "账号不得为空!";
            System.out.println("账号不得为空!");
            return "error";
        } else if (model.getPassword() == null || model.getPassword().equals("") && model.getUserid() != null && !model.getUserid().equals("")) {

            tip = "密码不得为空!";
            System.out.println("密码不得为空!");
            return "error";
        } else if (model.getPassword() == null || model.getPassword().equals("") || model.getUserid() == null || model.getUserid().equals("")) {
            tip = "账号、密码不得为空!";
            return "error";
        }


        model = userService.login(model);
        if (model == null) {
            tip = "登陆失败，账号或者密码错误!";
            logger.info("===登录失败，账号或者密码错误!===");
            return "login";
        } else {
            session.put("user", model);
            logger.info("===登陆成功!" + model.toString() + "===");
            if (session.get("goURL") == null) {

                return query();
            } else {
                return "error";
            }

        }
    }
}
